/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dymedia.aibo.mvp.ui.adapter;

import android.view.KeyEvent;
import android.view.View;

import com.dymedia.aibo.R;
import com.dymedia.aibo.app.MainApp;
import com.dymedia.aibo.mvp.model.Entity.Channel;
import com.dymedia.aibo.mvp.ui.holder.ChannelHolder;

import java.util.List;

import me.jessyan.art.base.BaseHolder;
import me.jessyan.art.base.DefaultAdapter;

/**
 * ================================================
 * 展示 {@link DefaultAdapter} 的用法
 * <p>
 * Created by JessYan on 09/04/2016 12:57
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public class ChannelAdapter extends DefaultAdapter<Channel> {
    public ChannelAdapter(List<Channel> infos) {
        super(infos);
    }
    private OnFocusChangedListener listener;
    private OnKeyChangedListener listenerKey;
    private OnClickedListener listenerClicked;

    public void setFocusChangeListener(OnFocusChangedListener l){
        listener = l;
    }
    public void setKeyChangeListener(OnKeyChangedListener l){
        listenerKey = l;
    }
    public void setClickedListener(OnClickedListener l){
        listenerClicked = l;
    }

    public void setInfos(List<Channel> infos) {
        this.mInfos = infos;
    }

    @Override
    public BaseHolder<Channel> getHolder(View v, int viewType) {
        ChannelHolder holder = new ChannelHolder(v);
        v.setOnFocusChangeListener((v1, hasFocus) -> {
            if (listener != null) {
                listener.onFocusChange(holder.getAdapterPosition(), hasFocus);
            }
        });
        v.setOnKeyListener((v12, keyCode, event) -> {
            if (listenerKey != null)
                return listenerKey.onKeyChange(v12, keyCode, event);
            return false;
        });
        v.setOnClickListener(v13 -> {
            if (listenerClicked != null) {
                listenerClicked.onClicked(v);
            }
        });
        v.getLayoutParams().height = ((MainApp.recycleViewHeightChannel-MainApp.padding) / MainApp.channelRows)-1;
        return holder;
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.recycle_list_channel;
    }

    public interface OnFocusChangedListener{
        void onFocusChange(int position, boolean hasFocus);
    }
    public interface OnKeyChangedListener{
        boolean onKeyChange(View v, int keyCode, KeyEvent event);
    }
    public interface OnClickedListener{
        void onClicked(View v);
    }
}
